package com.thinksns.sociax.t4.android.message;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.component.CustomTitle;
import com.thinksns.sociax.component.LeftAndRightTitle;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.ThinksnsAbscractActivity;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.model.SystemMessageModel;
import com.thinksns.sociax.thinksnsbase.activity.widget.EmptyLayout;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 * @author LeiYan
 * @describe 系统消息列表页
 * @date 2017/7/26
 * @contact email:907238552@qq.com
 */

public class SystemMsgListActivity extends ThinksnsAbscractActivity implements OnRefreshListener,OnLoadmoreListener {

    private final int PAGE_COUNT = 20;
    @BindView(R.id.empty_layout)
    EmptyLayout mEmptyLayout;

    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.pull_refresh_list)
    ListView mListView;
    private int max_id;
    private List<SystemMessageModel> mData;
    private SystemMessageListAdapter mListAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        mData = new ArrayList<>();
        max_id = 0;
        mListAdapter = new SystemMessageListAdapter(this, mData);
        mListView.setDivider(ContextCompat.getDrawable(this, R.drawable.line_space_gray_1px));
        mListView.setAdapter(mListAdapter);
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadmoreListener(this);
        mEmptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                max_id = 0;
                initData();
            }
        });
    }

    private void initData() {
        Thinksns.getApplication().getUsers().getSystemMessageList(max_id, PAGE_COUNT, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                smartRefreshLayout.finishRefresh();
                smartRefreshLayout.finishLoadmore();
                mEmptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                ToastUtils.showToast(R.string.net_fail);
                if (max_id == 0 && mData.isEmpty()) {
                    mEmptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                }
            }

            @Override
            public void onResponse(String response, int id) {
                smartRefreshLayout.finishRefresh();
                smartRefreshLayout.finishLoadmore();
                mEmptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                List<SystemMessageModel> data = new ArrayList<SystemMessageModel>();
                try {
                    JSONObject object = new JSONObject(response);
                    SystemMessageModel model;
                    if (1 == object.optInt("status")) {
                        JSONArray array = object.optJSONArray("data");
                        int length = array.length();
                        for (int i = 0; i < length; i++) {
                            model = new SystemMessageModel(array.optJSONObject(i));
                            data.add(model);
                        }
                    } else {
                        ToastUtils.showToast(object.optString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    if (max_id == 0) {
                        if (data.isEmpty()) {
                            mEmptyLayout.setErrorType(EmptyLayout.NODATA);
                        } else {
                            mData.clear();
                            mData.addAll(data);
                        }
                    } else if (!data.isEmpty()) {
                        mData.addAll(data);
                    }
                    mListAdapter.setDataList(mData);
                    /*set refresh mode*/
                    if (data.size() < PAGE_COUNT) {
                        smartRefreshLayout.setEnableLoadmore(false);
                    } else {
                        smartRefreshLayout.setEnableLoadmore(true);
                        smartRefreshLayout.setEnableRefresh(true);
                    }
                }
            }
        });

    }

    @Override
    public String getTitleCenter() {
        return getString(R.string.message_system);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_system_message_list;
    }

    @Override
    protected CustomTitle setCustomTitle() {
        return new LeftAndRightTitle(R.drawable.img_back, this);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        max_id = mListAdapter.getMaxId();
        initData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        max_id = 0;
        initData();
    }
}
